package com.benyi.byiee.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.benyi.byiee.dao.mappers.LanguageInfoMapper;
import com.benyi.byiee.model.db.LanguageInfo;
import com.benyi.util.Config;
import com.benyi.util.bean.ServiceException; 
import jakarta.annotation.Resource;


@Service
public class LanguageInfoService {
	private static Logger log = LoggerFactory.getLogger(LanguageInfoService.class);
	
	@Resource(name="config")
	private Config config; 
	
	@Autowired
	LanguageInfoMapper langMapper;
	
	
	public List<LanguageInfo>  queryList(String code){
		if(code !=null && code.length()>=1) {
			code = code.substring(0,1)+"%";
		}else {
			code="%";
		} 
		List<LanguageInfo> list = langMapper.queryList(code);
		return list;
	}
	
	public LanguageInfo save(LanguageInfo v) throws ServiceException {
		String code = v.getLcode();
		if(code==null || code.length()==0) {
			throw new ServiceException("null_code","Code can not be null");
		}
		try {
			LanguageInfo c = langMapper.selectByPrimaryKey(code); 
			if(c==null) {
				langMapper.insert(v);
			}else {
				langMapper.updateByPrimaryKey(v);
			} 
			c = langMapper.selectByPrimaryKey(code);
			return c;
		}catch(Exception e) {
			log.info("Exception", e);
			throw new ServiceException("save-exception",e.getMessage());
		} 
	}
	
	public int delete(LanguageInfo v) throws ServiceException { 
		try { 
			int r = langMapper.deleteByPrimaryKey(v.getLcode());  
			return r; 
		}catch(Exception e) {
			log.info("Exception", e);
			throw new ServiceException("cannot_remove",e.getMessage());
		} 
	}
	
	public LanguageInfo selectByCode(String code) {  
		LanguageInfo c = langMapper.selectByPrimaryKey(code);  
		return c; 
	}
}
